package com.sysman.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.HttpRequest;
import com.sysman.po.AdminlogininfoCustom;
import com.sysman.service.AdminlogininfoService;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年7月26日 下午9:08:28 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 JDK1.8
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Login")
public class LoginContorller {
	@Autowired
	private AdminlogininfoService adminlogininfoService;
	/**
	 * 登录界面初始化
	 * @return
	 */
	@RequestMapping(value="/login")
	public ModelAndView Login() {
		ModelAndView model = new ModelAndView();
		model.setViewName("View/Login/Login");
		return model;
	}
	/**
	 * 实现登陆类，验证查询结果不为空，设置session值
	 * @param adminlogininfoCustom
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="loginSubmit")
	public @ResponseBody Map<String,Object> loginSubmit(HttpServletRequest request) throws Exception {
		/*System.out.println("进入管理员登陆提交函数");
		System.out.println("输出接收到的用户名是："+adminlogininfoCustom.getAdminloginname());
		System.out.println("输出接收到的密码是："+adminlogininfoCustom.getAdminloginpwd());
		try {
			AdminlogininfoCustom BackadminlogininfoCustom = adminlogininfoService.getAdminlogininfo(adminlogininfoCustom);
			if(BackadminlogininfoCustom!=null) {
				System.out.println("登陆成功！");
				session.setAttribute("adminloginname", BackadminlogininfoCustom.getAdminloginname());
				session.setAttribute("adminloginpwd", BackadminlogininfoCustom.getAdminloginpwd());
				session.setAttribute("adminloginid", BackadminlogininfoCustom.getAdminloginid());
				return 1;
			}else{
				return 0;
			}
		} catch (Exception e) {
			System.out.println("查询错误！！"+e.getMessage());
			return -1;
		}*/
		//System.out.println("输出这个结果。。。"+BackadminlogininfoCustom);
		HttpSession session = request.getSession();
		String adminloginname = request.getParameter("adminloginname");
		String adminloginpwd = request.getParameter("adminloginpwd");
		String param = "adminloginname="+adminloginname+"&adminloginpwd="+adminloginpwd;
		Map<String,Object> result = new HashMap<>();
		String jsoncode = "";
		String jsonmsg = "";
		try {
			JSONObject json = HttpRequest.sendPost("login", param);
			jsoncode = json.get("code").toString();
			jsonmsg = json.get("msg").toString();
			System.out.println("输出这个json数据："+json);
			if("100".equals(jsoncode)) {
				try {
					JSONObject jsondata = json.getJSONObject("data");
					Integer adminloginid = jsondata.getInt("id");
					String adminloginpix = jsondata.get("adminloginpix").toString();
					session.setAttribute("adminloginname", adminloginname);
					session.setAttribute("adminloginpwd", adminloginpwd);
					session.setAttribute("adminloginid", adminloginid);
					session.setAttribute("adminloginpix", adminloginpix);
				} catch (Exception e) {
					System.out.println("数据解析出错了，出错信息是："+e.getMessage());
					jsoncode="-200";
					jsonmsg="数据解析错误！";
				}
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			jsoncode = "-100";
			jsonmsg = "数据传输出错了！";
		}
		result.put("code", jsoncode);
		result.put("msg", jsonmsg);
		
		return result;
	}
	//登出函数
	/**
	 * 登出类，销毁session中的值，实现登出
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/logoutsubmit")
	public String logoutsubmit(HttpSession session){
		session.invalidate();
		return "redirect:login.action";
	}
}
